package com.qf.taobei.service.impl;

import com.qf.taobei.entity.*;
import com.qf.taobei.entity.vo.ProductVo;
import com.qf.taobei.entity.vo.ProductVo2;
import com.qf.taobei.entity.vo.ViewProduct;
import com.qf.taobei.entity.vo.ViewProductType;
import com.qf.taobei.exception.ProductException;
import com.qf.taobei.mapper.ProductMapper;
import com.qf.taobei.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@SuppressWarnings("all")

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public RespBean queryAllProduct() {
        List<ProductVo> list = productMapper.queryAllProduct();
        if (list.size() == 0) {
//            return RespBean.respError("查询所有商品失败");
            throw new ProductException("查询所有商品失败");
        }
        return RespBean.respSuccess("获取成功", list);

    }

    @Override
    public RespBean queryProductByIdForAfter(Integer id) {
        ProductVo productvo = productMapper.queryProductByIdForAfter(id);
        if(productvo==null){
//            return RespBean.respError("找不到该商品");
            throw new ProductException("找不到该商品");
        }
        return RespBean.respSuccess("获取成功",productvo);
    }

    @Override
    public RespBean queryProductByIdForFront(Integer id) {
        ProductVo productvo = productMapper.queryProductByIdForFront(id);
        if(productvo==null){
            return RespBean.respError("找不到该商品");
        }
        return RespBean.respSuccess("获取成功",productvo);
    }

    @Override
    public RespBean addProduct(Product product, HttpServletRequest request) {
        Integer storeId = (Integer) request.getSession().getAttribute("storeId");
        Picture picture = new Picture();
        List urlList = (List) request.getSession().getAttribute("urlList");
        int i = productMapper.addProduct(product);
        if (i == 0) {
            return RespBean.respError("添加商品失败");
        }
        //记录存储个数
        int num = 0;
        for (int i1 = 0; i1 < urlList.size(); i1++) {
            picture.setUrl(urlList.get(i1).toString());
            picture.setPid(product.getId());
            productMapper.addPictureToDB(picture);
            num++;
        }

        Integer imgId = picture.getId();
        product.setImgId(imgId);
        product.setStoreId(storeId);
        productMapper.updateProductById(product);

        if (num != urlList.size()) {
            return RespBean.respError("图片存储失败");
        }

        return RespBean.respSuccess("添加商品成功");
    }

    @Override
    public RespBean updateProductById(Product product) {
        int i = productMapper.updateProductById(product);
        if (i == 0) {
            return RespBean.respError("更新商品失败");
        }
        return RespBean.respSuccess("更新商品成功");
    }

    @Override
    public RespBean delProductById(Long id) {
        int i = productMapper.delProductById(id);
        if (i == 0) {
            return RespBean.respError("删除商品失败");
        }
        //删除购物车对应的商品
        Integer integer = productMapper.deleteProductByPId(id);
        return RespBean.respSuccess("删除商品成功");
    }

    @Override
    public RespBean delProductByIdList(List idList) {
        int i = productMapper.delProductByIdList(idList);
        if (i == 0) {
            return RespBean.respError("删除商品失败");
        }
        return RespBean.respSuccess("删除商品成功");
    }

    @Override
    public RespBean upload(@RequestBody MultipartFile[] file, HttpServletRequest request) {
        RespBean respBean = new RespBean();
        List list = new ArrayList();
        for (int i = 0; i < file.length; i++) {
            //获取img路径
            String realPath = request.getServletContext().getRealPath("asserts/img");
            //创建目录
            File realPathFile = new File(realPath);
            if (!realPathFile.exists()) {
                realPathFile.mkdirs();
            }

            //获取原文件名
            String filename = file[i].getOriginalFilename();
            //拼接UUID解决重名
            filename = UUID.randomUUID() + "_" + filename;
            try {
                file[i].transferTo(new File(realPath, filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //生成文件访问路径
            filename = request.getScheme() + "://"
                    + request.getServerName() + ":"
                    + request.getServerPort()
                    + request.getContextPath()
                    + "/asserts/img/" + filename;
            list.add(filename);
        }
        request.getSession().setAttribute("urlList",list);
        respBean.setData(list);
        respBean.setStatus(200);
        return respBean;
    }

    @Override
    public RespBean queryProductByDim(Dim dim) {
        List<ProductVo> list = productMapper.queryProductByDim(dim);
        if (list.size() == 0) {
            return RespBean.respError("查询不到相关商品哦",list);
        }
        return RespBean.respSuccess("查询到的商品如下", list);
    }



    @Override
    public RespBean queryPictureByProductId(Integer id) {
        List<Picture> pictureList = productMapper.queryPictureByProductId(id);
        if (pictureList.size() == 0) {
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功", pictureList);
    }

    @Override
    public RespBean queryProductByParentId(Integer parentId) {
        List<ProductVo2> list = productMapper.queryProductByParentId(parentId);
        if (list.size() == 0) {
            return RespBean.respError("查询失败");
        }
        return RespBean.respSuccess("查询成功", list);
    }

    @Override
    public RespBean getProductListByFlag(String flag) {
        List<ProductVo> list = productMapper.getProductListByFlag(flag);
        if (list.size() == 0) {
            return RespBean.respError("暂无数据");
        }
        return RespBean.respSuccess("查询成功", list);
    }

    @Override
    public RespBean updateProductFlag(Integer id,Integer flag) {
        int i= productMapper.updateProductFlag(id,flag);
        if (i == 0) {
            return RespBean.respError("修改商品状态失败");
        }
        return RespBean.respSuccess("修改商品状态成功");
    }

    @Override
    public RespBean queryProductByStoreId(HttpServletRequest request) {
        Integer storeId = (Integer)request.getSession().getAttribute("storeId");
        List<ProductVo> list = productMapper.queryProductByStoreId(storeId);
        if (list.size() == 0) {
            return RespBean.respError("暂无数据");
        }
        return RespBean.respSuccess("查询成功", list);
    }

    @Override
    public List<ViewProductType> queryProductByProductTypeId(int id) {
        return productMapper.queryProductTypeById(id);
    }

    @Override
    public List<ViewProduct> getProductByParentId(int parentId) {
        return productMapper.getProductByParentId(parentId);
    }

    @Override
    public ViewProductType qeuryProductById(Integer id) {
        return productMapper.qeuryProductById(id);
    }

}
